package oa.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.alibaba.fastjson.JSON;

import oa.entity.Company;
import oa.entity.Message;
import oa.service.CompanyService;
import oa.service.MessageService;
import oa.service.impl.CompanyServiceImpl;
import oa.service.impl.MessageServiceImpl;

/**
 * Servlet implementation class Messageservice
 */
@WebServlet("/messageservice")
public class Messageservice extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public Messageservice() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		request.setCharacterEncoding("UTF-8");
		response.setCharacterEncoding("UTF-8");
		response.setContentType("text/html;charset=utf-8");
		HttpSession session = request.getSession();
		PrintWriter out = response.getWriter();
		String rid = request.getParameter("rid");
		String opt = request.getParameter("opt");
		String del=request.getParameter("del");
		MessageService me = new MessageServiceImpl();
		SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String date = simpleDateFormat.format(new Date());
		Date dates = null;
		try {
			dates = simpleDateFormat.parse(date);
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		if ("selectuser".equals(opt)) {

			CompanyService companyService = new CompanyServiceImpl();
			try {
				List<Company> companyList = companyService.selectAllCompany(0);
				List<Message> mes = me.selectMessage(Integer.parseInt(rid));
				session.setAttribute("mess", mes);
				session.setAttribute("companyList", companyList);
			
			} catch (Exception e) {
				// TODO: handle exception
			}	
			
			request.getRequestDispatcher("Select-Show/message_tabulation.jsp").forward(request, response);
		} else if ("add".equals(opt)) {

			boolean flag = false;
			Message mess = new Message(request.getParameter("val-title"), request.getParameter("val-content"),
					request.getParameter("val-type"), dates,Integer.parseInt(request.getParameter("val-company")));
			flag = me.addMessage(mess);
			out.print(JSON.toJSONString(flag));
		}else if("del".equals(opt)){
			boolean flag = false;
			
			flag=me.deleteMessage(Integer.parseInt(del));
			 out.print(JSON.toJSONString(flag));
		}
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
